package com.yang.service.wximpl;


import com.yang.bean.*;
import com.yang.bean.yvo.wx.KeywordBean;
import com.yang.bean.yvo.wx.SearchKeywordBean;
import com.yang.mapper.MarketKeyWordMapper;
import com.yang.mapper.MarketSearchHistoryMapper;
import com.yang.service.wx.WXSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 杨庆
 * @since: 2022.9.9 16:14
 * @description:
 */
@Service
public class WXSearchServiceIMpl implements WXSearchService {

    @Autowired
    MarketKeyWordMapper marketKeyWordMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;


    @Override
    public SearchKeywordBean selectIndex(MarketUser marketUser) {
        SearchKeywordBean searchKeywordBean = new SearchKeywordBean();

        //TODO 默认的搜索健
        MarketKeyWordExample example = new MarketKeyWordExample();
        MarketKeyWordExample.Criteria criteria = example.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        List<MarketKeyWord> list = marketKeyWordMapper.selectByExample(example);
        // 取出来一个就行了
        MarketKeyWord marketKeyWord = list.get(0);
        searchKeywordBean.setDefaultKeyword(marketKeyWord);

        //TODO 搜索的历史 根据用户的id查询搜索的历史，shiro整合以后在写
        //TODO 获取进来的就是用户的信息
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.setOrderByClause("add_time desc");
        MarketSearchHistoryExample.Criteria criteria2 = marketSearchHistoryExample.createCriteria();
        criteria2.andUserIdEqualTo(marketUser.getId());
        criteria2.andDeletedEqualTo(false);
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        //TODO 返回的结果只要关键字
        ArrayList<KeywordBean> keywords = new ArrayList<>();
        if (marketSearchHistories.size() > 0) {
            int i=0;
            for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
                KeywordBean keywordBean = new KeywordBean();
                keywordBean.setKeyword(marketSearchHistory.getKeyword());
                keywords.add(keywordBean);
                i+=1;
                if (i >= 5) {
                    break;
                }
            }
        }
        searchKeywordBean.setHistoryKeywordList(keywords);
        //TODO 热门的搜索健
        MarketKeyWordExample example1 = new MarketKeyWordExample();
        MarketKeyWordExample.Criteria criteria1 = example.createCriteria();
        criteria1.andIsHotEqualTo(true);
        List<MarketKeyWord> list1 = marketKeyWordMapper.selectByExample(example1);
        searchKeywordBean.setHotKeywordList(list1);
        return searchKeywordBean;
    }

    @Override
    public String[] selectByKeyword(String keyword,MarketUser marketUser) {
        //根据keyword模糊查询
        MarketKeyWordExample example = new MarketKeyWordExample();
        MarketKeyWordExample.Criteria criteria = example.createCriteria();
        criteria.andKeywordLike("%"+keyword+"%");
        List<MarketKeyWord> list = marketKeyWordMapper.selectByExample(example);
        int size = list.size();
        String[] strings = new String[size];
        int index=0;
        if ( size != 0) {
            for (MarketKeyWord marketKeyWord : list) {
                strings[index++]=marketKeyWord.getKeyword();
            }
        }
//        //搜索完成插入数据
//        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
//        marketSearchHistory.setAddTime(new Date());
//        marketSearchHistory.setFrom("wx");
//        marketSearchHistory.setKeyword(keyword);
//        marketSearchHistory.setUserId(marketUser.getId());
//        marketSearchHistoryMapper.insertSelective(marketSearchHistory);
        return strings;
    }

    @Override
    public void clearhistory(MarketUser marketUser) {
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setDeleted(true);
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(marketUser.getId());
        marketSearchHistoryMapper.updateByExampleSelective(marketSearchHistory,example);
    }
}
